/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myxa;

import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

/**
 *
 * @author Tuandn
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//        testDB2XA();
        testMSSSQLXA();
    }

    private static void testDB2XA() {
        XADataSource xaDS;
        XAConnection xaCon = null;
        XAResource xaRes = null;
        Xid xid = null;
        Connection con = null;
        Statement stmt = null;
        int ret;
        try {
            Logger.getLogger(Main.class.getName()).log(Level.INFO, "get xa connection");

            xaDS = DataSourceFactory.getDB2DataSource();
            xaCon = xaDS.getXAConnection(DataSourceFactory.DB2_USERNAME, DataSourceFactory.DB2_PASSWORD);
            xaRes = xaCon.getXAResource();
            con = xaCon.getConnection();
            stmt = con.createStatement();
            xid = new MyXid(100, new byte[0x01], new byte[0x02]);
            xaRes.start(xid, XAResource.TMNOFLAGS);

            Logger.getLogger(Main.class.getName()).log(Level.INFO, "insert new record into customer");

            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss-");
            stmt.executeUpdate("insert into sqlj.customer (email) values ('" + sdf.format(new Date(System.currentTimeMillis())) + "dntuan@yahoo.net')");
            xaRes.end(xid, XAResource.TMSUCCESS);
            ret = xaRes.prepare(xid);
            System.out.println("xa status: " + ret);
            if (ret == XAResource.XA_OK) {
                xaRes.commit(xid, false);
            }

            Logger.getLogger(Main.class.getName()).log(Level.INFO, "insert successfully");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                xaRes.rollback(xid);
            } catch (XAException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private static void testMSSSQLXA() {
        XADataSource xaDS = null;
        XAConnection xaCon = null;
        XAResource xaRes = null;
        Xid xid = null;
        Connection con = null;
        Statement stmt = null;
        int ret;
        try {
            Logger.getLogger(Main.class.getName()).log(Level.INFO, "get xa connection");

            xaDS = (XADataSource) DataSourceFactory.getMSSQLDataSource();
            xaCon = xaDS.getXAConnection(DataSourceFactory.MSSQL_USERNAME, DataSourceFactory.MSSQL_PASSWORD);
            xaRes = xaCon.getXAResource();
            con = xaCon.getConnection();
            stmt = con.createStatement();
            xid = new MyXid(100, new byte[0x01], new byte[0x02]);
            xaRes.start(xid, XAResource.TMNOFLAGS);

            Logger.getLogger(Main.class.getName()).log(Level.INFO, "insert new record into tblLog");

            stmt.executeUpdate("insert into tblLog(message, user_id) values ('message 21', 64)");
            xaRes.end(xid, XAResource.TMSUCCESS);
            ret = xaRes.prepare(xid);
            System.out.println("xa status: " + ret);
            if (ret == XAResource.XA_OK) {
                xaRes.commit(xid, false);
            }

            Logger.getLogger(Main.class.getName()).log(Level.INFO, "insert successfully");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                xaRes.rollback(xid);
            } catch (XAException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
